﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntityModel;
using Common;

namespace Services
{

    public class ProductService
    {
        #region***属性数据***

        ShopDbEntities bdContext = DbContextHelper.GetCurrentDbContext();
        CategoryService categoryService = new CategoryService();
        int show = (int)EnumModel.Status.Show;


        #endregion

        #region 获取所有产品

        /// <summary>
        /// 获取所有分类
        /// </summary>
        /// <returns></returns>
        ///  PagedList<Category>
        ///  
        public IQueryable<Product> GetAllList()
        {

            return bdContext.Products.OrderByDescending(c => c.Id);
        }

        #endregion

        #region 新增数据
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Create(Product model)
        {
            bdContext.Products.AddObject(model);
            bdContext.SaveChanges();
            return model.Id;
        }
        #endregion

        #region 根据Id获取实体
        /// <summary>
        /// 根据Id获取实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Product GetModel(int id)
        {
            return bdContext.Products.Include("Brand").Include("Category").Include("Method").Include("ProductAlbum").FirstOrDefault(p => p.Id == id);
        }
        #endregion

        #region 设置产品相册封面
        /// <summary>
        /// 设置产品相册封面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public void SetAlbumHome(int pid, int albumId)
        {
            Product product = bdContext.Products.FirstOrDefault(p => p.Id == pid);
            product.AlbumId = albumId;
            bdContext.SaveChanges();
        }
        #endregion

        #region 根据CategoryId获取产品
        /// <summary>
        /// 根据CategoryId获取产品
        /// </summary>
        /// <returns></returns>
        public IQueryable<Product> GetListByCategoryId(int cid)
        {
            return bdContext.Products.Include("ProductAlbum").Where(p => p.CategoryId == cid && p.Status == show).OrderByDescending(c => c.Id);
        }
        #endregion



        public void EditContents(string content)
        {
            Product model = bdContext.Products.Include("Brand").Include("Category").Include("Method").Include("ProductAlbum").FirstOrDefault(p => p.Id == 2);
            model.Contents = content;
            bdContext.SaveChanges();

        }

        public void Edit(Product model)
        {
            Product pModel = bdContext.Products.Include("Brand").Include("Category").Include("Method").Include("ProductAlbum").FirstOrDefault(p => p.Id == model.Id);
            pModel.Name = model.Name;
            pModel.ShortUrl = model.ShortUrl;
            pModel.ShowPlace = model.ShowPlace;
            pModel.Sku = model.Sku;
            pModel.Status = model.Status;
            pModel.SalePrice = model.SalePrice;
            pModel.Quantity = model.Quantity;
            pModel.MethodId = model.MethodId;
            pModel.BrandId = model.BrandId;
            pModel.CategoryId = model.CategoryId;
            pModel.BeginTime = model.BeginTime;
            pModel.Contents = model.Contents;
            pModel.MetaKeywords = model.MetaKeywords;
            pModel.MetaDescription = model.MetaDescription;
            pModel.IsPost = model.IsPost;
            pModel.EndTime = model.EndTime;
            pModel.CreateOn = model.CreateOn;
            pModel.CostPrice = model.CostPrice;
            bdContext.SaveChanges();

        }
        public IQueryable<Product> GetAllPruductById(int id)
        {
            Category c = categoryService.GetModel(id);
            IList<int> ids = new List<int>();
            var list = categoryService.FindChiledRoles(ids, c).AsQueryable<int>();
            return bdContext.Products.Include("Brand").Include("Category").Include("Method").Include("ProductAlbum").Where(p =>p.Status == show&& list.Contains(p.CategoryId ?? 0)).OrderByDescending(p => p.DisplayOrder); ;
        }

    }
}
